package config

import (
    "github.com/go-ini/ini"
    "strconv"
    "strings"
    "time"
    "wwgin/global/variable"
)

type Mysql struct {
    SlowThreshold           time.Duration //慢SQL阈值(sql执行时间超过此时间单位（秒），就会触发系统日志记录)
    Deploy                  int           //是否开启主从模式1是0否
    SlaveNum                int           //从服务器数量,一个填1
    Master                  MasterConf
    Slave                   []SlaveConf
    SlaveSetMaxIdleConns    int           //从库 最大闲置连接数
    SlaveSetMaxOpenConns    int           //从库 最大打开的连接数
    SlaveSetConnMaxLifetime time.Duration //从库 连接不活动时的最大生存时间(秒)
}

type MasterConf struct {
    Host               string
    DataBase           string
    Port               int
    User               string
    Pass               string
    Charset            string
    SetMaxIdleConns    int           //最大闲置连接数
    SetMaxOpenConns    int           //最大打开的连接数
    SetConnMaxLifetime time.Duration //连接不活动时的最大生存时间(秒)
}

type SlaveConf struct {
    Host     string
    DataBase string
    Port     int
    User     string
    Pass     string
    Charset  string
}

var MysqlConf Mysql

var MysqlConfMap = map[string]Mysql{}

func init() {
    switch variable.Env {
    case "online": //生产环境

        //def的默认主库配置
        var master_conf = MasterConf{
            Host:               "c797b4c1eac6492a8f6a806fdaa37e61in01.internal.cn-north-4.mysql.rds.myhuaweicloud.com",
            DataBase:           "iphome",
            Port:               3306,
            User:               "iphome",
            Pass:               "O6hVxsUjv8dT",
            Charset:            "utf8mb4",
            SetMaxIdleConns:    10,
            SetMaxOpenConns:    128,
            SetConnMaxLifetime: 60,
        }
        //配置从库 - 可以配置多个
        var slave_conf []SlaveConf
        slave_conf = append(slave_conf, SlaveConf{
            Host:     "c797b4c1eac6492a8f6a806fdaa37e61in01.internal.cn-north-4.mysql.rds.myhuaweicloud.com",
            DataBase: "iphome",
            Port:     3306,
            User:     "iphome",
            Pass:     "O6hVxsUjv8dT",
            Charset:  "utf8mb4",
        })

        MysqlConfMap["def"] = Mysql{
            SlowThreshold:           2,
            Deploy:                  0,
            SlaveNum:                1,
            Master:                  master_conf,
            Slave:                   slave_conf,
            SlaveSetMaxIdleConns:    100,
            SlaveSetMaxOpenConns:    1000,
            SlaveSetConnMaxLifetime: 60,
        }

    default:
        //def的默认主库配置
        var master_conf = MasterConf{
            Host:               "127.0.0.1",
            DataBase:           "ip_service",
            Port:               3306,
            User:               "root",
            Pass:               "root",
            Charset:            "utf8mb4",
            SetMaxIdleConns:    10,
            SetMaxOpenConns:    128,
            SetConnMaxLifetime: 60,
        }
        //配置从库 - 可以配置多个
        var slave_conf []SlaveConf
        slave_conf = append(slave_conf, SlaveConf{
            Host:     "127.0.0.1",
            DataBase: "ip_service",
            Port:     3306,
            User:     "root",
            Pass:     "root",
            Charset:  "utf8mb4",
        })
        slave_conf = append(slave_conf, SlaveConf{
            Host:     "127.0.0.1",
            DataBase: "ip_service",
            Port:     3306,
            User:     "root",
            Pass:     "root",
            Charset:  "utf8mb4",
        })
        MysqlConfMap["def"] = Mysql{
            SlowThreshold:           2,
            Deploy:                  0,
            SlaveNum:                1,
            Master:                  master_conf,
            Slave:                   slave_conf,
            SlaveSetMaxIdleConns:    100,
            SlaveSetMaxOpenConns:    1000,
            SlaveSetConnMaxLifetime: 60,
        }
        /*
        //bdata的数据库配置,连接后直接到model配置就可以使用了
        var bdata_master_conf = MasterConf{
            Host:               "127.0.0.1",
            DataBase:           "test",
            Port:               3306,
            User:               "root",
            Pass:               "phpts",
            Charset:            "utf8mb4",
            SetMaxIdleConns:    10,
            SetMaxOpenConns:    128,
            SetConnMaxLifetime: 60,
        }
        //配置从库 - 可以配置多个
        var bdata_slave_conf []SlaveConf
        bdata_slave_conf = append(bdata_slave_conf, SlaveConf{
            Host:     "127.0.0.1",
            DataBase: "test",
            Port:     3306,
            User:     "root",
            Pass:     "phpts",
            Charset:  "utf8mb4",
        })
        MysqlConfMap["bdata"] = Mysql{
            SlowThreshold:           2,
            Deploy:                  1,
            SlaveNum:                1,
            Master:                  bdata_master_conf,
            Slave:                   bdata_slave_conf,
            SlaveSetMaxIdleConns:    100,
            SlaveSetMaxOpenConns:    1000,
            SlaveSetConnMaxLifetime: 60,
        }
        */


        //更多的数据库的配置信息

    }


    //添加检查配置文件是否存在,存在的话,配置已配置文件为准
    load, err := ini.Load(variable.ROOT_PATH + "/config/config.ini")
    if err == nil {

        //添加def库的mysql配置
        mysql := load.Section("mysql")
        host := mysql.Key("Host").String() //有主库host才去读取配置
        if host != "" {
            //主库
            master_conf := MasterConf{
                Host:               mysql.Key("Host").MustString(MysqlConfMap["def"].Master.Host),
                DataBase:           mysql.Key("DataBase").MustString(MysqlConfMap["def"].Master.DataBase),
                Port:               mysql.Key("Port").MustInt(MysqlConfMap["def"].Master.Port),
                User:               mysql.Key("User").MustString(MysqlConfMap["def"].Master.User),
                Pass:               mysql.Key("Pass").MustString(MysqlConfMap["def"].Master.Pass),
                Charset:            mysql.Key("Charset").MustString(MysqlConfMap["def"].Master.Charset),
                SetMaxIdleConns:    mysql.Key("SetMaxIdleConns").MustInt(MysqlConfMap["def"].Master.SetMaxIdleConns),
                SetMaxOpenConns:    mysql.Key("SetMaxOpenConns").MustInt(MysqlConfMap["def"].Master.SetMaxOpenConns),
                SetConnMaxLifetime: mysql.Key("SetConnMaxLifetime").MustDuration(MysqlConfMap["def"].Master.SetConnMaxLifetime),
            }
            //从库 - 从库根据配置是否开启,然后从宽是一个逗号分割的字符串
            mysql_slave := load.Section("mysql_slave")
            var slave_conf []SlaveConf
            status := mysql_slave.Key("Status").MustBool(false)
            if status { //读取配置信息
                Host := strings.Split(mysql_slave.Key("Host").MustString(""), ",")
                DataBase := strings.Split(mysql_slave.Key("DataBase").MustString(""), ",")
                Port := strings.Split(mysql_slave.Key("Port").MustString(""), ",")
                User := strings.Split(mysql_slave.Key("User").MustString(""), ",")
                Pass := strings.Split(mysql_slave.Key("Pass").MustString(""), ",")
                Charset := strings.Split(mysql_slave.Key("Charset").MustString(""), ",")

                if len(Host) > 0 { //以第一个为准判断
                    for k, _ := range Host {
                        port, _ := strconv.Atoi(Port[k])
                        slave_conf = append(slave_conf, SlaveConf{
                            Host:     Host[k],
                            DataBase: DataBase[k],
                            Port:     port,
                            User:     User[k],
                            Pass:     Pass[k],
                            Charset:  Charset[k],
                        })
                    }
                }
            }
            MysqlConfMap["def"] = Mysql{
                SlowThreshold:           2,
                Deploy:                  0,
                SlaveNum:                1,
                Master:                  master_conf,
                Slave:                   slave_conf,
                SlaveSetMaxIdleConns:    mysql_slave.Key("SetMaxIdleConns").MustInt(MysqlConfMap["def"].SlaveSetMaxIdleConns),
                SlaveSetMaxOpenConns:    mysql_slave.Key("SlaveSetMaxOpenConns").MustInt(MysqlConfMap["def"].SlaveSetMaxOpenConns),
                SlaveSetConnMaxLifetime: mysql_slave.Key("SlaveSetConnMaxLifetime").MustDuration(MysqlConfMap["def"].SlaveSetConnMaxLifetime),
            }
        } else {
            delete(MysqlConfMap, "def")
        }

        //复制上面的代码 添加更多库的mysql配置 ...
    }
}
